// pages/login/login.js
var timer = require('../../utils/timer.js');
const app = getApp();
const api = require('../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        // 
        isPravicyChecked: false,
        verifyCode: '', //6617
        captchaLabel: '获取验证码',
        seconds: timer.length,
        captchaDisabled: false,
        form:{
          phone:'',
        }
    },
// 
captcha: function(e) {
    let form = this.data.form;
    let param = {
        phone: form.phone
    };
  if (!(/^1[3456789]\d{9}$/.test(form.phone))) {
      wx.showToast({
        title: '手机号有误',
        icon: 'error',
        duration: 2000
      })
   
  } else{
      // 禁用按钮点击
      this.setData({
        captchaDisabled: true
      });
      // 立刻显示重发提示，不必等待倒计时启动
      this.setData({
        captchaLabel: timer.length + '秒后重新发送'
      });
      // 启动以1s为步长的倒计时
      var interval = setInterval(() => {
        timer.countdown(this);
      }, 1000);
      // 停止倒计时
      setTimeout(function() {
        clearInterval(interval);
      }, timer.length * 1000);

      if (this.data.seconds == timer.length) {
      console.log(param)
        api.send_sms(param).then(function(res){
            console.log(param)
        })
        wx.showToast({
            title: '发送成功'
        });
      }
  }
},
pravicyTap(){
  this.setData({
    isPravicyChecked: !this.data.isPravicyChecked
  })
},
login: function(){
  if(!this.data.isPravicyChecked){
    wx.showToast({
      title: '请同意隐私政策',
      icon: 'none'
    })
    return
  }
  let form = this.data.form;
  let param = {
    phone: form.phone,
    code: form.code
  }
  api.login(param).then(function(res){
      if(res.code == '1'){
        wx.showToast({
          title: res.data,
          icon: 'error'
        });
      }else{
        wx.showToast({
          title: '登录成功！'
        });
        wx.setStorageSync('userinfo', res.data)
        app.globalData.userinfo = res.data;
        wx.switchTab({
          url: '/pages/my/my',
        })
      }
  })
  
},

phoneValue: function(e){
  const that = this
  let form = this.data.form;
  form.phone = e.detail.value
  that.setData({
    form
  })
},

codeValue: function(e){
  const that = this
  let form = this.data.form;
  form.code = e.detail.value
  that.setData({
    form
  })
},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})